package com.whf.android.jar.view.item;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.whf.android.jar.app.Latte;


/**
 * RecyclerView 加上水印
 *
 * @author qf
 * @version 1.0.3
 */
public final class WatermarkUtils {

    private static WatermarkDecoration mWatermarkDecoration = null;

    /**
     * <p>实现步骤：
     * <p>1、自定义一个Drawable用于动态显示水印。
     * <p>2、用ItemDecoration来绘制水印，并且跟踪滚动位置。
     * <p>3、由于是重写ItemDecoration的onDraw（），所以该水印的位置在ItemView之下，如果ItemView有背景色将会遮挡住水印
     *
     * @param mRecyclerView:控件上绘制动态水印
     */
    public static void setWatermark(RecyclerView mRecyclerView) {
        if (mWatermarkDecoration == null) {
            mWatermarkDecoration = new WatermarkDecoration.Builder(Latte.watermark())
                    .setColumnNum(3)
                    .setTextColor(0x22666666)
                    .setTextSize(35)
                    .builder();
        }
        if (mRecyclerView != null) {
            mRecyclerView.addItemDecoration(mWatermarkDecoration);
            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    //设置水印滚动位置
                    if (mWatermarkDecoration != null) {
                        mWatermarkDecoration.setScrollY(dy);
                    }
                }
            });
        }
    }
}
